package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.commons.result.util.ObjectTransfer;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.converter.OrderConverter;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import com.mall.order.utils.ExceptionProcessorUtils;
import com.mall.shopping.constants.ShoppingRetCode;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverter orderConverter;

    //Aaron
    @Override
    public OrderListResponse queryOrder(OrderListRequest request) {
        OrderListResponse response = new OrderListResponse();
        try {
            request.requestCheck();
            PageHelper.startPage(request.getPage(),request.getSize());
            Example orderExample = new Example(Order.class);
            Example.Criteria criteria = orderExample.createCriteria();
            orderExample.setOrderByClause(request.getSort() + " " + "desc");
            criteria.andEqualTo("userId",request.getUserId());
            List<Order> orders = orderMapper.selectByExample(orderExample);
            PageInfo<Order> pageInfo = new PageInfo<>(orders);
            Long total = pageInfo.getTotal();
            response.setTotal(total);
            List<OrderDetailInfo> orderDetailInfos = new ArrayList<>();
            for (Order order : orders) {
                List<OrderItem> orderItems = orderItemMapper.queryByOrderId(order.getOrderId());
                List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
                OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(order.getOrderId());
                OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
                OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order,orderItemDtos,orderShippingDto);
                orderDetailInfos.add(orderDetailInfo);
            }
            response.setData(orderDetailInfos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.info(e.getMessage());
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }

    @Override
    public OrderDetailResponse orderDetail(OrderDetailRequest id) {
        String orderId = id.getOrderId();
        OrderDetailResponse orderDetailResponse = new OrderDetailResponse();
        try {
            Order order = orderMapper.selectByPrimaryKey(orderId);
            List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
            List<OrderItemDto> orderItemDtos = orderConverter.item2dto(orderItems);
            OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            orderDetailResponse = orderConverter.order2res(order);
            orderDetailResponse.setOrderItemDto(orderItemDtos);
            orderDetailResponse.setOrderShippingDto(orderShippingDto);
            orderDetailResponse.setCode(OrderRetCode.SUCCESS.getCode());
            orderDetailResponse.setMsg(OrderRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            orderDetailResponse.setCode(OrderRetCode.SYSTEM_ERROR.getCode());
            orderDetailResponse.setMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
        }
        return orderDetailResponse;
    }

//    @SneakyThrows
//    @Override
//    public OrderDetail orderDetail(OrderDetailRequest id) {
//
//        String orderId = id.getOrderId();
//
//        Order order = orderMapper.selectByPrimaryKey(orderId);
//
//        OrderDetail orderDetail = new OrderDetail();
//        orderDetail.setUserId(order.getUserId());
//        orderDetail.setUserName(order.getBuyerNick());
//        orderDetail.setOrderStatus(order.getStatus());
//
//        Example example = new Example(OrderItem.class);
//        example.createCriteria().andEqualTo("orderId",id.getOrderId());
//        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
//        HashMap<String, String> stringStringHashMap = new HashMap<>();
//        stringStringHashMap.put("orderStatus","status");
//        ArrayList<OrderDetail.GoodsListBean> goodsListBeans = new ArrayList<>();
//        Double total = 0D;
//        for (OrderItem orderItem : orderItems) {
//            OrderDetail.GoodsListBean change = ObjectTransfer.change(orderItem, OrderItem.class, OrderDetail.GoodsListBean.class,stringStringHashMap);
//
//            goodsListBeans.add(change);
//            total+=orderItem.getNum()*orderItem.getPrice();
//        }
//        orderDetail.setOrderTotal(total);
//        orderDetail.setGoodsList(goodsListBeans);
//        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(id.getOrderId());
//        if (orderShipping.getReceiverMobile()!=null){orderDetail.setTel(orderShipping.getReceiverMobile().toString());}
//        else {
//            orderDetail.setTel(orderShipping.getReceiverPhone());
//        }
//
//        orderDetail.setStreetName(orderShipping.getReceiverAddress());
//
//        return orderDetail;
//    }

}
